import { ref, reactive, computed } from 'vue';
import type { ResponseData } from '../utils/request';

/**
 * 分页参数接口
 */
export interface PaginationParams {
  page: number;
  size: number;
  [key: string]: any;
}

/**
 * 分页数据接口
 */
export interface PaginationData<T> {
  list: T[];
  total: number;
  page: number;
  size: number;
  pages: number;
}

/**
 * 分页请求钩子函数
 * @param requestFn 请求函数
 * @param defaultParams 默认参数
 * @param immediate 是否立即执行
 */
export default function usePagination<T = any>(
  requestFn: (params: PaginationParams) => Promise<ResponseData<PaginationData<T>>>,
  defaultParams: Partial<PaginationParams> = {},
  immediate: boolean = false
) {
  // 加载状态
  const loading = ref(false);
  
  // 刷新状态
  const refreshing = ref(false);
  
  // 加载更多状态
  const loadingMore = ref(false);
  
  // 错误信息
  const error = ref<Error | null>(null);
  
  // 列表数据
  const list = ref<T[]>([]);
  
  // 总数
  const total = ref(0);
  
  // 当前页码
  const page = ref(1);
  
  // 每页大小
  const size = ref(10);
  
  // 总页数
  const pages = ref(0);
  
  // 查询参数
  const params = reactive<PaginationParams>({
    page: 1,
    size: 10,
    ...defaultParams
  });
  
  // 是否有更多数据
  const hasMore = computed(() => {
    return page.value < pages.value;
  });
  
  // 是否为空数据
  const isEmpty = computed(() => {
    return total.value === 0;
  });
  
  // 加载数据
  const load = async (reset: boolean = false): Promise<ResponseData<PaginationData<T>>> => {
    // 如果重置，则重置页码
    if (reset) {
      params.page = 1;
    }
    
    // 设置加载状态
    if (reset) {
      refreshing.value = true;
    } else if (params.page > 1) {
      loadingMore.value = true;
    } else {
      loading.value = true;
    }
    
    error.value = null;
    
    try {
      const res = await requestFn(params);
      
      // 更新数据
      if (reset || params.page === 1) {
        list.value = res.data.list as T[];
      } else {
        list.value = [...list.value, ...res.data.list] as T[];
      }
      
      total.value = res.data.total;
      page.value = res.data.page;
      size.value = res.data.size;
      pages.value = res.data.pages;
      
      return res;
    } catch (err) {
      error.value = err as Error;
      throw err;
    } finally {
      loading.value = false;
      refreshing.value = false;
      loadingMore.value = false;
    }
  };
  
  // 刷新数据
  const refresh = async (): Promise<ResponseData<PaginationData<T>>> => {
    return load(true);
  };
  
  // 加载更多数据
  const loadMore = async (): Promise<ResponseData<PaginationData<T>> | null> => {
    if (!hasMore.value || loadingMore.value) {
      return null;
    }
    
    params.page++;
    return load();
  };
  
  // 重置参数
  const resetParams = (newParams: Partial<PaginationParams> = {}) => {
    Object.keys(params).forEach(key => {
      if (key !== 'page' && key !== 'size') {
        delete params[key];
      }
    });
    
    Object.assign(params, { page: 1, size: params.size }, newParams);
  };
  
  // 设置参数
  const setParams = (newParams: Partial<PaginationParams> = {}) => {
    Object.assign(params, newParams);
  };
  
  // 如果需要立即执行
  if (immediate) {
    load();
  }
  
  return {
    loading,
    refreshing,
    loadingMore,
    error,
    list,
    total,
    page,
    size,
    pages,
    params,
    hasMore,
    isEmpty,
    load,
    refresh,
    loadMore,
    resetParams,
    setParams
  };
} 